GBML manual / Collision

tile_free

True if this instance’s whole collision box, standing at (x, y), would be clear of the room’s own solid ground - the tile grid and any obstacles the room has. The room-as-wall twin of pos_free: use it to ask "may I step here?" for anything that moves.

Syntax

tile_free(x, y) -> bool

Arguments

NameType
xreal
yreal

Returns

bool

Description

True if this instance could stand at (x, y) without any part of it being inside the room's own solid ground. It is pos_free for the case where the thing in your way is the room itself - the painted tiles - and not an object.

x and y are a place in the room, counted from the room's top-left corner, the same way this instance's own x and y are counted. Nothing moves. The game only asks "if I stood there, would I be stuck?"

It checks the whole box, not one point

This is the difference from tile_solid. tile_solid(x, y) looks at one tile, the one under the single point you give it. An instance is not a point. It has a collision box (see Overlap and collision), and a 16 by 16 box covers four tiles at once, because tiles are only 8 by 8. If the box is not lined up with the grid it can cover nine. Ask about one point and the other corners can walk straight into a wall.

tile_free uses the same box as pos_free: the one set by mask_w, mask_h, mask_x and mask_y, or by the sprite if you set none of them. It moves that box so the instance's x and y would be (x, y), then looks up every tile the box touches. Each lookup goes straight to the tile, so the answer takes a handful of lookups, never a search through all your instances. It does not get slower as the level grows.

What counts as blocked

A solid instance is not part of this. A wall made from obj_wall objects is still a job for pos_free. Tiles are the room's own ground, and tile_free reads only that.

Example

if (tile_free(x + hsp, y)) {
    x += hsp;
}
if (tile_free(x, y + vsp)) {
    y += vsp;
}

Moves the walker one direction at a time. Each step happens only if the walker's whole box would be clear of solid ground at the new place. Splitting it into two steps means that when one direction is blocked, the walker slides along the wall instead of stopping dead. Swap tile_free(x + hsp, y) for pos_free(x + hsp, y, obj_wall) and you have the same code for walls made of objects.

On the GBA

Tests the BOX, not a point: the same box mask_w/mask_h/mask_x/mask_y (or mask_sprite/sprite) describe, moved so the instance’s own x and y would be (x, y) - exactly what pos_free moves. A 16 by 16 box covers about four 8-pixel tiles (up to nine when it is not lined up with the grid), and every one of them is looked up directly - a handful of lookups, never a search through instances, so it does not get slower as a level grows. It is the same check move_slide makes, so "free" here means move_slide would not be stopped there. The BODY question: TILE_SOLID and TILE_LOW_COVER both block, only TILE_PASS is free - there is no projectile twin, because a projectile is a point (see tile_blocks_projectile). The part of the box outside the room blocks nothing: the edge of a room is not a wall. A room with no collision layer has no grid to block anything, so every place is free. An instance with no box (no sprite and no mask size) is free everywhere: neither the tiles nor the obstacles are asked about it. (pos_free is different: it can find a solid instance with the point strictly inside.)

See also: tile_solidpos_freetile_wallmove_slidetile_classtile_blocks_projectileOverlap and collisionSlopes